package examstarter;

import gui.GUIMain;

import java.awt.GridLayout;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import structure.Course;
import structure.Exam;

public class GUIStarterRight extends JPanel{
	
	private static final long serialVersionUID = 1L;
	private static final int MAX_COURSE = 4;
	
	private Timer timer;
	
	private GUIStarter parent;
	private ArrayList<GUIItem> itemArray;
	

	public GUIStarterRight(GUIStarter parent) {
		this.parent = parent;
		setOpaque(false);
		Border border = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
		setBorder(BorderFactory.createTitledBorder(border, "Danh sách Khoá thi"));
		
		itemArray = new ArrayList<>();
		setLayout(new GridLayout(2, 2, 4, 4));
		timer = new Timer();
		timer.scheduleAtFixedRate(new TimerTask() {
			
			@Override
			public void run() {
				checkTime();
			}
		}, 0, 1000l);
	}
	
	private void checkTime(){
		for(int i = 0; i < itemArray.size();i++){
			long cd = itemArray.get(i).getCooldown();
			if(cd == -10000){
				continue;
			}
			if(cd > 0){
				itemArray.get(i).setCooldown(cd - 1);
			}
			if(cd <= 0){
				GUIMain.getDb().UpdateCourse(itemArray.get(i).getCourse().getCourseID(), 
						itemArray.get(i).getCourse().getCourseTitle(),
						itemArray.get(i).getCourse().getDateOfExam(),
						itemArray.get(i).getCourse().getTimeOfExam(), 
						itemArray.get(i).getCourse().getRoom(), 
						true);
				parent.removeACourse(itemArray.get(i).getCourse().getCourseID(),itemArray.get(i).getNumofQuestion());
				JOptionPane.showMessageDialog(null, "Khoá thi " + itemArray.get(i).getCourse().getCourseTitle() + " đã hoàn thành. Hệ thống sẽ tiến hành chấm điểm, và thông báo tới thí sinh.");
				itemArray.get(i).setCooldown(-10000);
				itemArray.remove(i);
				displayItem();
				parent.getLeftPanel().refresh();
			}
		}
	}
	
	public void addItem(Course course, Exam exam){
		if(itemArray.size() >= MAX_COURSE){
			JOptionPane.showMessageDialog(parent, "Số lượng khoá thi đã đạt mức lớn nhất.",
					"Thông báo", JOptionPane.ERROR_MESSAGE);
			return;
		}
		for(GUIItem count : itemArray){
			if(count.getCourse().getCourseID() == course.getCourseID()){
				JOptionPane.showMessageDialog(parent, "Khoá thi không thể thi 2 đề cùng lúc",
						"Thông báo", JOptionPane.ERROR_MESSAGE);
				return;
			}
		}
		int temp;
		try {
			temp = GUIMain.getDb().getQuestionsByExam(exam.getExamID()).size();
			itemArray.add(new GUIItem(this,course, exam, temp));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		displayItem();
	}
	
	public void startItem(Course course, Exam exam){
		parent.startACourse(course, exam);
	}
	
	public void removeItem(Course course, Exam exam){
		if(itemArray.size() == 0){
			return;
		}
		
		for(int i = 0 ; i < itemArray.size();i++){
			if(itemArray.get(i).getCourse().getCourseID() == course.getCourseID() 
					&& itemArray.get(i).getExam().getExamID() == exam.getExamID()){
				itemArray.remove(i);
			}
		}
		displayItem();
	}
	
	public void clear(){
		itemArray.clear();
		displayItem();
	}
	
	private void displayItem(){
		removeAll();
		for(GUIItem count : itemArray){
			add(count);
		}
		revalidate();
		repaint();
	}
}
